/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 28.07.2008
*/

package it.cums.ui.wizards;

import it.cums.core.model.Order;
import it.cums.core.model.OrderedProduct;
import it.cums.core.util.Converter;
import it.cums.ui.localization.Messages;

import org.eclipse.jface.wizard.Wizard;

public class OrderedProductWizard extends Wizard {
	private OrderedProductWizardPage orderedProductWizardPage;
	private Order order;
	private OrderedProduct product;
	
	public OrderedProductWizard(Order order) {
		super();
		this.order = order;
		setNeedsProgressMonitor(true);
		setWindowTitle(Messages.OrderedProductWizard_0);
	}
	
	public OrderedProductWizard(OrderedProduct product) {
		super();
		this.product = product;
		setNeedsProgressMonitor(true);
		setWindowTitle(Messages.OrderedProductWizard_1);
	}

	
	public void addPages() {
		if (product == null) {
			orderedProductWizardPage = new OrderedProductWizardPage();
		} else {
			orderedProductWizardPage = new OrderedProductWizardPage(product);
		}
		addPage(orderedProductWizardPage);
	}
	
	
	public boolean performFinish() {
		if (product == null) {
			product = new OrderedProduct();
			product.setProduct(orderedProductWizardPage.getProduct());
			product.setPrice(Converter.convertToDouble(orderedProductWizardPage.getPrice()));
			product.setQuantity(Converter.convertToInteger(orderedProductWizardPage.getQuantity()));
			product.setTotalPrice(Converter.convertToDouble(orderedProductWizardPage.getTotalPrice()));
			product.setDiscount(Converter.convertToInteger(orderedProductWizardPage.getDiscount()));
			product.setEndPrice(Converter.convertToDouble(orderedProductWizardPage.getEndPrice()));
			order.getOrderedProducts().addOrderedProduct(product);
			product.setOrder(order);
		} else {
			product.setProduct(orderedProductWizardPage.getProduct());
			product.setPrice(Converter.convertToDouble(orderedProductWizardPage.getPrice()));
			product.setQuantity(Converter.convertToInteger(orderedProductWizardPage.getQuantity()));
			product.setTotalPrice(Converter.convertToDouble(orderedProductWizardPage.getTotalPrice()));
			product.setDiscount(Converter.convertToInteger(orderedProductWizardPage.getDiscount()));
			product.setEndPrice(Converter.convertToDouble(orderedProductWizardPage.getEndPrice()));
		}
		return true;
	}
}
